//
//  LGMedicineBallScreenFooter.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/8/22.
//

#import "LGMedicineBallScreenFooter.h"
#import "DatePickerView.h"
@interface LGMedicineBallScreenFooter()
/**   */
@property (nonatomic , strong)UIView *lineView;
/**   */
@property (nonatomic , strong)NSDateFormatter *formatter;

@end

@implementation LGMedicineBallScreenFooter

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.startTF];
        [self addSubview:self.endTF];
        [self addSubview:self.lineView];
        [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.startTF.mas_right).offset(viewPix(12));
            make.right.equalTo(self.endTF.mas_left).offset(-viewPix(12));
            make.centerY.equalTo(self.startTF);
            make.height.equalTo(@(1));
        }];
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(selectDateAction:)]];
    }
    return self;
}

-(void)selectDateAction:(UITapGestureRecognizer *)sender{
    __weak typeof(self) weakSelf = self;
    CGPoint point = [sender locationInView:self];
    if(CGRectContainsPoint(self.startTF.frame, point)){
        //开始时间
        DatePickerView *pickerView = [[DatePickerView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
        pickerView.title = @"起始时间";
        [[UIApplication sharedApplication].keyWindow addSubview:pickerView];
        pickerView.selectDate = ^(NSString *date) {
            if([weakSelf dateIsCorrect:date end:weakSelf.endTF.text] == YES){
                weakSelf.startTF.text = date;
                if(weakSelf.screenDateSelectStart){
                    weakSelf.screenDateSelectStart(weakSelf.key, date);
                }
            }else{
                [TooltipView showMessage:@"开始时间不能晚于结束时间" offset:0];
            }
        };
        
    }else if (CGRectContainsPoint(self.endTF.frame, point)){
        //结束时间
        DatePickerView *pickerView = [[DatePickerView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
        pickerView.title = @"结束时间";
        [[UIApplication sharedApplication].keyWindow addSubview:pickerView];
        pickerView.selectDate = ^(NSString *date) {
            if([weakSelf dateIsCorrect:weakSelf.startTF.text end:date] == YES){
                weakSelf.endTF.text = date;
                if(weakSelf.screenDateSelectEnd){
                    weakSelf.screenDateSelectEnd(weakSelf.key, date);
                }
            }else{
                [TooltipView showMessage:@"终止时间不能早于开始时间" offset:0];
            }
        };
    }
}

-(BOOL)dateIsCorrect:(NSString *)startDate  end:(NSString *)endDate{
    if(startDate.length == 0 || endDate.length == 0) return YES;
    NSDate *start = [self.formatter dateFromString:startDate];
    NSDate *end = [self.formatter dateFromString:endDate];
    NSComparisonResult result = [start compare:end];
    if(result == NSOrderedAscending || result == NSOrderedSame){
        return YES;
    }
    return NO;
}

-(NSDateFormatter *)formatter{
    if(!_formatter){
        _formatter = [[NSDateFormatter alloc] init];
        _formatter.dateFormat = @"yyyy-MM-dd";
    }
    return _formatter;
}

-(UITextField *)startTF{
    if(!_startTF){
        _startTF = [[UITextField alloc]initWithFrame:CGRectMake(0, viewPix(12), (Screen_W-viewPix(83))/2.0, viewPix(32))];
        _startTF.backgroundColor = [UIColor colorWithString:@"#F6F6F6"];
        _startTF.textColor = [UIColor colorWithString:@"#666666"];
        _startTF.font = LGFontWeight(15, UIFontWeightMedium);
        _startTF.textAlignment = NSTextAlignmentCenter;
        _startTF.userInteractionEnabled = NO;
        _startTF.cornerRidus = viewPix(16);
        _startTF.placeholder = @"起始时间";
    }
    return _startTF;
}

-(UITextField *)endTF{
    if(!_endTF){
        _endTF = [[UITextField alloc]initWithFrame:CGRectMake(kMaxX(self.startTF.frame)+viewPix(33), viewPix(12), (Screen_W-viewPix(83))/2.0, viewPix(32))];
        _endTF.backgroundColor = [UIColor colorWithString:@"#F6F6F6"];
        _endTF.textColor = [UIColor colorWithString:@"#666666"];
        _endTF.font = LGFontWeight(15, UIFontWeightMedium);
        _endTF.textAlignment = NSTextAlignmentCenter;
        _endTF.userInteractionEnabled = NO;
        _endTF.cornerRidus = viewPix(16);
        _endTF.placeholder = @"终止时间";
    }
    return _endTF;
}

-(UIView *)lineView{
    if(!_lineView){
        _lineView = [[UIView alloc]init];
        _lineView.backgroundColor = [UIColor colorWithString:@"#999999"];
    }
    return _lineView;
}

@end
